package cn.hhhy.service.impl;

import cn.hhhy.mapper.*;
import cn.hhhy.pojo.*;
import cn.hhhy.service.UserService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private VillageMapper villageMapper;
    @Autowired
    private EstateMapper estateMapper;
    @Autowired
    private User_VillageMapper userVillageMapper;
    @Autowired
    private CouponMapper couponMapper;
    @Override
    public User login(User user) {
            return userMapper.selectOne(Wrappers.lambdaQuery(User.class)
                    .eq(User::getUname, user.getUname())
                    .eq(User::getPassword, DigestUtils.md5DigestAsHex(user.getPassword().getBytes()))
            );
    }

    // 分页查询
    @Override
    public PageResult findPage(QueryPageBean pageBean) {
        Integer currentPage = pageBean.getCurrentPage();
        Integer pageSize = pageBean.getPageSize();
        String queryString = pageBean.getQueryString();

        IPage userPage = userMapper.selectPage(new Page<>(currentPage, pageSize), Wrappers.lambdaQuery(User.class)
                .like(!Strings.isEmpty(queryString), User::getUname, queryString)
                .or()
                .like(!Strings.isEmpty(queryString), User::getTel, queryString)
                .or()
                .like(!Strings.isEmpty(queryString), User::getIdentity, queryString)
        );
        return new PageResult(userPage.getTotal(),userPage.getRecords());
    }

    // 添加新用户
    @Override
    public Result add(Map<String, Object> users) {
        String identity = (String) users.get("identity");
        String uname = (String) users.get("uname");
        String password = (String) users.get("password");
        Integer type;
        if (users.get("type") == null) {
            type = 1;
        } else {
            type = Integer.valueOf((String) users.get("type"));
        }
        String tel = (String) users.get("tel");
        Integer vid = Integer.valueOf((String) users.get("vid"));
        Integer building_no = Integer.valueOf((String) users.get("building_no"));
        Integer house_number = Integer.valueOf((String) users.get("house_number"));

        Village village = villageMapper.selectById(vid);
        if (village == null) return new Result(false,"小区查找失败");

        User user = new User();
        user.setIdentity(identity);
        user.setUname(uname);
        user.setTel(tel);
        user.setPassword(DigestUtils.md5DigestAsHex(password.getBytes()));
        user.setState(1);
        user.setType(type);
        userMapper.insert(user);
        User_Village userVillage = new User_Village();
        userVillage.setUid(user.getUid());
        userVillage.setVid(vid);
        userVillageMapper.insert(userVillage);

//      添加新用户的房产
        Estate estate = new Estate();
        estate.setUid(user.getUid());
        estate.setVid(vid);
        estate.setBuildingNo(building_no);
        estate.setHouseNumber(house_number);
        estateMapper.insert(estate);
        // 分发新用户优惠券
        Coupon coupon = new Coupon();
        coupon.setUid(user.getUid());
        coupon.setState(0);
        coupon.setCname("新用户折扣券");
        coupon.setCabout("欢迎新用户购买车位");
        coupon.setPreferenceType(0);
        coupon.setDiscountAmount(0.89);

        couponMapper.insert(coupon);



        return new Result(true,"添加用户成功");
    }

    @Override
    public void delete(Integer id) {
        estateMapper.delete(Wrappers.lambdaQuery(Estate.class).eq(Estate::getUid,id));
        userVillageMapper.delete(Wrappers.lambdaQuery(User_Village.class).eq(User_Village::getUid,id));
        userMapper.deleteById(id);
    }

    @Override
    public void update(User user) {
        user.setPassword(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()));
        userMapper.updateById(user);
    }

    @Override
    public User findById(Integer id) {
        return userMapper.selectById(id);
    }
}
